import { createStore, Plugin, PluginFunctions } from "../../core";

import Open, { OpenProps } from "./Open";
import OpenMenuItem from "./OpenMenuItem";

import StoreProps from "./StoreProps";
import { ReactElement, useMemo } from "react";

interface OpenPlugin extends Plugin {
  Open: (props: OpenProps) => ReactElement;
  OpenButton: () => ReactElement;
  OpenMenuItem: () => ReactElement;
}

const openPlugin = (): OpenPlugin => {
  const store = useMemo(() => createStore<StoreProps>({}), []);

  const OpenDecorator = (props: OpenProps) => <Open {...props} store={store} />;

  const OpenButtonDecorator = () => <OpenDecorator />;

  const OpenMenuItemDecorator = () => (
    <OpenDecorator>{(p) => <OpenMenuItem onClick={p.onClick} />}</OpenDecorator>
  );

  return {
    install: (pluginFunctions: PluginFunctions) => {
      store.update("openFile", pluginFunctions.openFile);
    },
    Open: OpenDecorator,
    OpenButton: OpenButtonDecorator,
    OpenMenuItem: OpenMenuItemDecorator,
  };
};

export default openPlugin;
